package com.ruoyi.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.domain.BankLogOne;
import com.ruoyi.mapper.BankLogOneMapper;
import com.ruoyi.service.BankLogOneService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class BankLogOneServiceImpl implements BankLogOneService {

    @Resource
    private BankLogOneMapper bankOneMapper;

    @Override
    public BankLogOneMapper initDao() {
        return bankOneMapper;
    }

    @Override
    public BankLogOne selectSerialNumber(String  serialNumber) {
        return bankOneMapper.selectSerialNumber(serialNumber);
    }


    @Override
    public String importExcel(List<BankLogOne> bankList, Boolean updateSupport) {

        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (BankLogOne item : bankList) {
            try {
                BankLogOne b = bankOneMapper.selectSerialNumber(item.getSerialNumber());
                if (StringUtils.isNull(b)) {
                    bankOneMapper.insert(item);
                    successNum++;
                    System.out.println(item);
                    successMsg.append("<br/>" + successNum + "、流水号 " + item.getSerialNumber() + " 导入成功");
                } else if (updateSupport) {
                    bankOneMapper.update(item);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、流水号 " + item.getSerialNumber() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、流水号 " + item.getSerialNumber() + " 已存在");
                }

            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、流水号 " + item.getSerialNumber() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());

            }
        }
            if (failureNum > 0) {
                failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
                throw new BusinessException(failureMsg.toString());
            } else {
                successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
            }

            return successMsg.toString();

    }


}
